<?php
namespace App\Http\Controllers\Admin;

use App\Utilitys\BaiduyunOcr;
use Illuminate\Http\Request;

class TestAiController extends BaseController
{
    //测试AI接口

    /**
     * 测试识别
     * 百度云文字识别
     */
    public function bdocr()
    {
        $imglink = 'https://gsai.qzwlyx.cn/upload/20240903/172534174066d6a02c00a4b.png';
        //获取鉴权Access_token
        $res = BaiduyunOcr::getAccessToken();
        if ($res['code'] < 0) {
            return $this->toJson(-2, '鉴权失败', $res['data']);
        }
        $access_token = $res['data']['access_token'];
        //sleep(1); //休眠1秒
        //通用文字标准版
        $res = BaiduyunOcr::getTextFromImg($access_token, $imglink);
        if ($res['code'] < 0) {
            return $this->toJson(-2, '调用失败', $res['data']);
        }
        $words = $res['data']['words_result'];
        if (empty($words)) {
            return $this->toJson(-2, '没有文字信息');
        }
        return $this->toJson(0, '', $words);
    }

    /**
     * 阿里云通义千问
     * key：aec6489d-c3de-966e-a026-73e4192eb08b
     */
    public function altyqw(Request $request)
    {
        $input = $request->post('input', '');
        if (!$input) {
            return $this->toJson(-2, '输入值必须的');
        }
        $httpUrl = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions';
        $apikey = 'sk-8e5decbabd8244ba9d143e0a05d66c07';
        $headers = [
            'Authorization: Bearer '.$apikey,
            'Content-Type: application/json',
        ];
        $jsonData = [
            "model" => "qwen-plus",
            "messages" => [
                [
                    "role" => "system",
                    "content" => "You are a helpful assistant."
                ],
                [
                    "role" => "user",
                    "content" => $input,
                ]
            ]
        ];
        $jsonData = json_encode($jsonData);
        $res = $this->toCurl($httpUrl, $jsonData, 'post', $headers, 1);
        return $this->toJson(-2, '', [$httpUrl, $jsonData, $res]);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 构建CURL请求，单线程
     * @param $url 请求地址
     * @param $string 参数拼接的字符串
     * @param $method 请求方式：put、post、get、delete
     * @param $headers 头信息
     * @return $json
     */
    public function toCurl($url = '', $string = '', $method = 'get', $headers = [], $isdd = 0)
    {
//        echo '<pre>';
//        var_dump($url); echo '<br/>';
//        var_dump($string); echo '<br/>';
        if (!$url) {
            return array('code'=>-2 ,'msg'=>'没有接口地址');
        }
        if (!$method) {
            return array('code'=>-2 ,'msg'=>'没有请求方式');
        }
        $curl = curl_init(); //初始化
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if ($method == 'post') {
            curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
            curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
            curl_setopt($curl, CURLOPT_ENCODING, '');
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $string);
            if (!empty($headers)) {
                curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            } else {
                curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json', 'Content-Length: ' . strlen($string)
                ));
            }
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
        if ($isdd) {
            ddd($output);
        }
        if (!$output) {
            return array('code'=>-1, 'msg'=>'请求失败', 'data' => $output);
        }
        $items = json_decode($output,true);
        if (!isset($items['output']['text']) || !$items['output']['text']) {
            return array('code' => 2, 'msg' => '未找到模型', 'data' => $items);
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $items);
    }
}